#include <iostream>
#include <fstream>
#include <string>
using namespace std;

struct StudentInformation
{
	char *id;
	char *name;
	char *birthday;
	char *sex;
	char *bodyHealth;
	string s;

	StudentInformation *next;
	StudentInformation (StudentInformation *ptr = NULL){
		id = new char[20];
		name = new char[20];
		birthday = new char[20];
		sex = new char[20];
		bodyHealth = new char[20];
		next = ptr;
	}
	StudentInformation (char *a,char *b,char *c,char *d,char *e,StudentInformation *ptr=NULL)
	{
		id = a;
		name = b;
		birthday = c;
		sex = d;
		bodyHealth = e;
		next = ptr;
	}
	void input(){
		cin>>id;
		cin>>name;
		cin>>birthday;
		cin>>sex;
		cin>>bodyHealth;
	}
	void output(){
		cout<<id;
		cout<<endl<<name;
		cout<<endl<<birthday;
		cout<<endl<<sex;
		cout<<endl<<bodyHealth<<endl;
	}
};
ostream & operator << ( ostream & out, const StudentInformation* x )
{
    out << "学号:"<< x->id << endl;
    out << "姓名:"<< x->name  << endl;
    out << "性别:"<< x->sex << endl;
    out << "出生日期:"<< x->birthday << endl;
    out << "健康状况:"<<x->bodyHealth << endl;
    return out;
}

class List{
public:
	List(){
		first = new StudentInformation();
	}
	bool Insert(int i ,char *id1,char *name1,char* birthday1,char* sex1,char* bodyHealth1);
	void Print();
	void InsertRear(char* endTag);
	StudentInformation* Located(int i);
	bool Delete(int i);
	StudentInformation* Search(char *);
	void Read();
	void Write();
private:
	StudentInformation *first;
}; 




int main (){
	char a[20],b[20],c[20],d[20],e[20];
	List l1;
	int choice = 1;
	char i[20];
	cout<<"endTag"<<endl;
	cin>>i;
	while(choice)
	{
		cout<<"请输入您要进行的操作："<<endl;
		cout<<"1	建立新的学生健康表"<<endl
			<<"2	插入学生信息"<<endl
			<<"3	删除学生信息"<<endl
			<<"4	查询学生信息（按学生学号来进行查找）"<<endl
			<<"5	在屏幕中输出全部学生信息"<<endl
			<<"6    从文件中读取健康表信息"<<endl
			<<"7    向文件写入学生健康表信息"<<endl
			<<"0	退出"<<endl;
        cin>>choice;
		switch(choice)
		{			
		case 1: 
		{
		cout<<"按学号、姓名、生日、性别、健康情况输入学生信息"<<endl;
		l1.InsertRear(i);
		break; 	
		}
		case 2:
		{
			cout<<"按学号、姓名、生日、性别、健康情况输入学生信息"<<endl;
			cin>>a>>b>>c>>d>>e;
			cout<<"请输入要插入的位置"<<endl;
			int k;
			cin>>k;
			l1.Insert(k,a,b,c,d,e);
			l1.Print();
			break;
		}
		case 3:{
			cout<<"请输入要删除的结点"<<endl;
			int k;
			bool flag;
			cin>>k;
			flag = l1.Delete(k);
			if (flag == true){
				cout<<"删除成功"<<endl;
			}
			else {
				cout<<"删除失败"<<endl;
			}
			break;
		}
		case 4:{
			char ID[20];
			cout<<"请输入学号进行查找"<<endl;
			cin>>ID;
			StudentInformation* l = l1.Search(ID);
			cout<<"该学生信息为"<<endl;
			l->output();
			break;
		}
		case 5:
		{
			l1.Print();
			break;
		}
		case 6:{
			l1.Read();
		}
		case 7:{
			l1.Write();
		}
	 	case 0: break; 

		default: cout<<"输入错误!请重新输入。"<<endl;
		}
	}
	return 0;
}
void List::Print(){
	StudentInformation *current = first->next;
	while (current != NULL){
		current->output();
		current = current->next;
	}
}
void List::InsertRear(char* endTag){
	StudentInformation *newNode,*last;
	char *listElement;
	char id1[20];
	char name1[20];
	char birthday1[20];
	char sex1[20];
	char bodyHealth1[20];
	last = first;
	int element;
	int RanElement;
	
	cin>>id1>>name1>>birthday1>>sex1>>bodyHealth1;
	while (strcmp(id1,endTag) != 0){
		newNode = new StudentInformation();
		if (newNode == NULL){cout<<"wrong"<<endl;exit(1);}
		strcpy(newNode->id,id1);
		strcpy(newNode->name,name1);
		strcpy(newNode->birthday,birthday1);
		strcpy(newNode->sex,sex1);
		strcpy(newNode->bodyHealth,bodyHealth1);
		last->next = newNode;
		last = newNode;
		cout<<"是否退出结束字符为n,将学号设置成n就可以退出输入"<<endl;
		cout<<"按学号、姓名、生日、性别、健康情况输入学生信息"<<endl;
		cin>>id1>>name1>>birthday1>>sex1>>bodyHealth1;
	}
	last->next = NULL;
}
StudentInformation* List::Located(int i){
	if (i < 0){
		return NULL;
	}
	StudentInformation *current = first;
	int k = 0;
	while (current != NULL&&k<i){
		current = current->next;
		k++;
	}
	return current;
}
bool List::Insert(int i,char *id1,char *name1,char* birthday1,char* sex1,char* bodyHealth1){//s插入在i个结点之后
	StudentInformation *current = Located(i);
	if (current == NULL){return false;}
	StudentInformation *newNode = new StudentInformation();
	if (newNode == NULL){
		cout<<"failed"<<endl;
		exit(1);
	}
	strcpy(newNode->id,id1);
	strcpy(newNode->name,name1);
	strcpy(newNode->birthday,birthday1);
	strcpy(newNode->sex,sex1);
	strcpy(newNode->bodyHealth,bodyHealth1);
	newNode ->next = current ->next;
	current ->next = newNode;
	return true;
}
bool List::Delete(int i){
	StudentInformation *current = Located(i-1);
	if (current == NULL||current->next == NULL){
		return false;
	}
	StudentInformation *del = current ->next;
	cout<<"待删除结点为:"<<endl;
	del->output();
	cout<<"是否要删除，删除请按1，退出请按2"<<endl;
	int k;
	cin>>k;
	if (k == 1){
		current ->next = del ->next;
		delete del;
		return true;
	}
	else {
		cout<<"取消删除动作"<<endl;
		return false;
	}
}
StudentInformation* List::Search(char *ID){
	StudentInformation* current = first->next;
	while (current != NULL){
		if (strcmp(current->id, ID) == 0){
			break;
		}
		else {
			current = current->next;
		}
	}
	return current;
}

void List::Write()
{
	ofstream fop;								 //以二进制方式打开stu.txt文件
	fop.open("stu.txt",ios::out|ios::binary|ios::trunc);
	if(fop.fail())                     //文件打开失败
	{
		cout<<"文件打开失败！"<<endl;
	}
	StudentInformation *current = first->next;
	while (current != NULL)       //把各字符信息写入文件
	{
		fop << current;
		current = current ->next;
	}
	cout<<"文件写入成功"<<endl;
}
void List::Read()
{
	ifstream fip;        //以二进制方式打开stu.txt文件
	fip.open("stu.txt",ios::binary|ios::in);
	if(fip.fail())       //文件打开失败
	{
		cout<<"文件打开失败！"<<endl;
	}
	char s;
	while(fip.get(s))
	{
		cout<<s;
		// fip.read((char*)&s,sizeof(s));   //从文件中读入一个学生的信息
		// if(!fip)
		// {
		// 	break;	
		// }
		// student.ReadInsert(s);
	}
	fip.close();
	cout<<"读写成功"<<endl;
}








